package com.yifang.yibingproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifang.yibingproject.dto.SysMenuDTO;
import com.yifang.yibingproject.dto.SysMenuTreeDTO;
import com.yifang.yibingproject.exception.ParamException;
import com.yifang.yibingproject.service.SysMenuService;
import com.yifang.yibingproject.utils.Log;
import com.yifang.yibingproject.utils.Result;
import com.yifang.yibingproject.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统菜单")
@RestController
@RequestMapping("sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @Log(value = "新增菜单", resp = true)
    @ApiOperation("新增菜单")
    @PostMapping("add")
    public Result<String> add(@ApiParam("菜单对象") @RequestBody SysMenuDTO menu) {
        if (menu.getParentId() == null)
            throw new ParamException("父级id为空");
        menu.setId(null);
        return ResultUtils.data(sysMenuService.add(menu));
    }

    @Log(value = "删除菜单", resp = true)
    @ApiOperation("删除菜单")
    @DeleteMapping("del/{id}")
    public Result<String> del(@ApiParam("分类id") @PathVariable String id) {
        return ResultUtils.data(sysMenuService.del(id));
    }

    @ApiOperation("获取菜单")
    @GetMapping("get/{id}")
    public Result<SysMenuDTO> get(@ApiParam("菜单id") @PathVariable String id) {
        return ResultUtils.data(sysMenuService.get(id));
    }

    @Log(value = "修改菜单", resp = true)
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result<String> update(@ApiParam("菜单编辑对象DTO") @RequestBody SysMenuDTO dto) {
        if (dto.getParentId() == null)
            throw new ParamException("父级id为空");
        return ResultUtils.data(sysMenuService.update(dto));
    }


    @ApiOperation("菜单列表")
    @GetMapping("list")
    public Result<Page<SysMenuDTO>> list(@ApiParam("页码") @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                         @ApiParam("数量") @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                         @ApiParam("父级id") @RequestParam(required = false) String parentId) {
        return ResultUtils.page(sysMenuService.list(pageNo, pageSize, parentId));
    }


    @ApiOperation("获取当前用户角色的树形菜单")
    @GetMapping("listMy/{id}")
    public Result<List<SysMenuTreeDTO>> listMy(@ApiParam("用户ID") @PathVariable Integer id) {
        return ResultUtils.data(sysMenuService.listMy(id));
    }

    @ApiOperation("获取所有树形菜单")
    @GetMapping("listTree/{type}")
    public Result<List<SysMenuTreeDTO>> listTree(@ApiParam("类型 1-带路宝 2-会员管理 3-供应商后台 4-服务商后台") @PathVariable Integer type) {
        return ResultUtils.data(sysMenuService.listTree(type));
    }

    @Log(value = "菜单升序", resp = true)
    @ApiOperation("菜单升序")
    @PutMapping("up/{id}")
    public Result<String> memberUp(@ApiParam("菜单id") @PathVariable String id) {
        return ResultUtils.data(sysMenuService.up(id));
    }

    @Log(value = "菜单降序", resp = true)
    @ApiOperation("菜单降序")
    @PutMapping("down/{id}")
    public Result<String> down(@ApiParam("菜单id") @PathVariable String id) {
        return ResultUtils.data(sysMenuService.down(id));
    }

    @ApiOperation("获取父级菜单")
    @GetMapping("getParent/{id}")
    public Result<List<SysMenuDTO>> getParent(@ApiParam("菜单id") @PathVariable String id) {
        return ResultUtils.data(sysMenuService.getParent(id));
    }

    @Log(value = "菜单隐藏设置", resp = true)
    @ApiOperation("设置是否隐藏")
    @PutMapping("setHidden")
    public Result<String> setHidden(@ApiParam("id") @RequestParam(required = false) String id,
                                    @ApiParam("是否隐藏") @RequestParam(required = false) Boolean hidden) {
        return ResultUtils.data(sysMenuService.setHidden(id, hidden));
    }







}
